package com.etime.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Content-Disposition", "attachment;filename=school.jpg");
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("学校.jpg");
    ServletOutputStream out = resp.getOutputStream();
    byte[] array = new byte[1024];
    int len = 0;
    while ((len=in.read(array))!=-1){
      out.write(array,0,len);
    }
    out.close();
    in.close();
  }
}
